package zx.x.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import zx.x.common.utils.PageUtils;
import zx.x.common.utils.R;
import zx.x.modules.app.entity.OrderEntity;
import zx.x.modules.app.entity.UserEntity;
import zx.x.modules.app.service.TbUserService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 用户
 *
 * @author x
 * @email x
 * @date 2022-12-14 20:40:33
 */
@RestController
@RequestMapping("/yh/user")
public class SysTbUserController {
    @Autowired
    private TbUserService tbUserService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(UserEntity user){
        Page page = PageHelper.startPage(user.getPage(),user.getLimit());
        QueryWrapper<UserEntity> queryWrapper = null;
        if (!"".equals(user.getUsername()) && user.getUsername() != null){
            queryWrapper = new QueryWrapper<UserEntity>();
            queryWrapper.like("username",user.getUsername());
        }
        List<UserEntity> list = tbUserService.list(queryWrapper);
        return R.ok().put("page", list).put("pages",page.getPages()).put("total",page.getTotal());
    }


    /**
     * 信息
     */
    @RequestMapping("/getInfo")
    public R info(Long id){
		UserEntity tbUser = tbUserService.getById(id);
        tbUser.setToken(null);
        tbUser.setOpenid(null);
        tbUser.setSessionKey(null);
        tbUser.setCode(null);
        return R.ok().put("user", tbUser);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("app:tbuser:save")
    public R save(@RequestBody UserEntity tbUser){
		tbUserService.save(tbUser);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(UserEntity tbUser){
		tbUserService.updateById(tbUser);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("app:tbuser:delete")
    public R delete(@RequestBody Long[] userIds){
		tbUserService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/updateTx")
    public R updateTx(UserEntity tbUser){
        tbUser.setTx("shanghutest/e171cb19d9f28016bec634e42e2e8d71");
        tbUserService.updateById(tbUser);
        return R.ok();
    }
}
